<template>
  <div>
    <div class="header">
      <div class="nav-topbar">
        <div class="container">
          <div class="topbar-menu">
            <a href="">小米商城</a>
            <a href="">MUI</a>
            <a href="">云服务</a>
            <a href="">协议规则</a>
          </div>
          <div class="topbar-user">
            <a href="javascript:;" v-if="username">{{username}}</a>
            <a href="javascript:;" @click="login" v-if="!username">登录</a>
            <a href="javascript:;" @click="logout" v-if="username">退出</a>
            <a href="javascript:;" v-if="username">我的订单</a>
            <a href="javascript:;" v-else>注册</a>
            <a href="javascript:;" class="my-cart" @click="goToCart"><span
                class="icon-cart"></span>购物车({{cartcount}})</a>
          </div>
        </div>
      </div>
      <div class="nav-header">
        <div class="container">
          <div class="header-logo">
            <a href="/#/index"></a>
          </div>
          <div class="header-menu">
            <div class="item-menu">
              <span>小米手机</span>
              <div class="children">
                <ul>
                  <li class="product" v-for="(item,index) in productList" :key="index">
                    <a :href="'/#/product/'+item.id" target="_blank">
                      <div class="pro-img"><img :src='item.mainImage' alt=""></div>
                      <div class="pro-name">{{item.name}}</div>
                      <div class="pro-title">{{currency(item.price)}}</div>
                    </a>
                  </li>

                </ul>
              </div>
            </div>
            <div class="item-menu">
              <span>RedMi红米</span>

            </div>
            <div class="item-menu">
              <span>电视</span>
              <div class="children">
                <ul>
                  <li class="product">
                    <a href="" target="_blank">
                      <div class="pro-img"><img v-lazy="'/imgs/nav-img/nav-3-1.jpg'" alt=""></div>
                      <div class="pro-name">小米壁画电视</div>
                      <div class="pro-title">6999元</div>
                    </a>
                  </li>
                  <li class="product">
                    <a href="" target="_blank">
                      <div class="pro-img"><img v-lazy="'/imgs/nav-img/nav-3-2.jpg'" alt=""></div>
                      <div class="pro-name">小米全面电视</div>
                      <div class="pro-title">1999元</div>
                    </a>
                  </li>
                  <li class="product">
                    <a href="" target="_blank">
                      <div class="pro-img"><img v-lazy="'/imgs/nav-img/nav-3-3.png'" alt=""></div>
                      <div class="pro-name">小米电视4A 32英寸</div>
                      <div class="pro-title">1799元</div>
                    </a>
                  </li>
                  <li class="product">
                    <a href="" target="_blank">
                      <div class="pro-img"><img v-lazy="'/imgs/nav-img/nav-3-4.jpg'" alt=""></div>
                      <div class="pro-name">小米CC9</div>
                      <div class="pro-title">1799元</div>
                    </a>
                  </li>
                  <li class="product">
                    <a href="" target="_blank">
                      <div class="pro-img"><img v-lazy="'/imgs/nav-img/nav-3-5.jpg'" alt=""></div>
                      <div class="pro-name">小米CC9</div>
                      <div class="pro-title">1799元</div>
                    </a>
                  </li>
                  <li class="product">
                    <a href="" target="_blank">
                      <div class="pro-img"><img v-lazy="'/imgs/nav-img/nav-3-6.png'" alt=""></div>
                      <div class="pro-name">查看全部</div>
                      <div class="pro-title">查看全部</div>
                    </a>
                  </li>
                </ul>
              </div>
            </div>
          </div>
          <div class="header-search">
            <div class="warpper">
              <input type="text" name="keyword">
              <a href="javascript:;"></a>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { mapState } from 'vuex'

export default {
  name: 'nav-header',
  // 定义属性
  data () {
    return {
      productList: '',
    }
  },
  computed: {
    // username () {
    //   return this.$store.state.username
    // },
    // cartcount () {
    //   return this.$store.state.cartcount
    // }
    ...mapState(['username', 'cartcount'])
  },
  // 监控data中的数据变化
  watch: {},
  // 方法集合
  methods: {
    //获取商品列表
    getproductlist () {
      this.axios.get('/products', {
        params: {
          categoryId: '100012',
          pageSize: 6,
        }
      }).then(res => {
        this.productList = res.list.slice(0, 6)

      })
    },
    //获取购物车数量 
    getCartCount () {
      this.axios.get('/carts/products/sum').then((res = 0) => {
        this.$store.dispatch('saveCartCount', res)
      })
    },
    currency (val) {
      if (!val) return '0.00';
      return '￥' + val.toFixed(2) + '元';
    },
    //前往购物车页面
    goToCart () {
      this.$router.push('/cart')
    },
    //前往登陆页面
    login () {
      this.$router.push('/login')
    },
    //退出登录
    logout () {
      this.axios.post('/user/logout').then(res => {
        this.$message.success('退出登录')
        this.$cookie.set('userId', '', { expires: '-1' })
        this.$store.dispatch('saveUserName', '')
        this.$store.dispatch('saveCartCount', 0)
      })
    }

  },
  // 生命周期 - 创建完成（可以访问当前this实例）
  created () {

  },
  // 生命周期 - 挂载完成（可以访问DOM元素）
  mounted () {
    this.getproductlist()
    if (this.$route.params.from === 'login' && this.$route.params) {
      this.getCartCount()
    }

  },
  beforeCreate () { }, // 生命周期 - 创建之前
  beforeMount () { }, // 生命周期 - 挂载之前
  beforeUpdate () { }, // 生命周期 - 更新之前
  updated () { }, // 生命周期 - 更新之后
  beforeDestroy () { }, // 生命周期 - 销毁之前
  destroyed () { }, // 生命周期 - 销毁完成
  activated () { }, // 如果页面有keep-alive缓存功能，这个函数会触发

}
</script>

<style lang="scss">
@import "../assets/scss/base.scss";
@import "../assets/scss/mixin.scss";
@import "../assets/scss/config.scss";
.header {
  .nav-topbar {
    height: 39px;
    line-height: 39px;
    background-color: #333333;
    color: #b0b0b0;
    .container {
      @include flex();
      a {
        display: inline-block;
        color: #b0b0b0;
        margin-right: 17px;
      }
      .my-cart {
        width: 110px;
        background-color: #ff6600;
        text-align: center;
        color: #ffffff;
        margin-right: 0px;
        .icon-cart {
          @include bgImg(16px, 12px, "/imgs/icon-cart-checked.png");
          margin-right: 4px;
        }
      }
    }
  }
  .nav-header {
    .container {
      position: relative;
      height: 112px;
      @include flex();
      .header-menu {
        display: inline-block;
        width: 643px;
        margin-left: 209px;
        .item-menu {
          display: inline-block;
          color: #333333;
          font-weight: bold;
          font-size: 16px;
          line-height: 112px;
          margin-right: 20px;
          span {
            cursor: pointer;
          }
          &:hover {
            color: $colorA;
            .children {
              opacity: 1;
              height: 220px;
              transition: all 0.5s;
            }
          }
          .children {
            height: 0;
            overflow: hidden;
            opacity: 0;
            position: absolute;
            top: 112px;
            left: 0px;
            width: 1226px;
            box-shadow: 0px 7px 6px 0px rgba(0, 0, 0, 0.11);
            z-index: 10;
            background-color: #ffffff;
            .product {
              position: relative;
              text-align: center;
              font-size: 12px;
              line-height: 12px;
              float: left;
              width: 16.6%;
              height: 220px;
              &:before {
                content: " ";
                border-left: 1px solid $colorF;
                position: absolute;
                top: 28px;
                right: 0;
                width: 1px;
                height: 100px;
              }
              &:last-child:before {
                display: none;
              }
              a {
                display: inline-block;
              }
              img {
                margin-top: 26px;
                height: 111px;
                width: auto;
              }
              .pro-img {
                height: 137px;
              }
              .pro-name {
                margin-top: 19px;
                margin-bottom: 8px;
                color: $colorB;
                font-weight: bold;
              }
              .pro-title {
                color: $colorA;
              }
            }
          }
        }
      }
      .header-search {
        width: 319px;
        .warpper {
          height: 50px;
          border: 1px solid #e0e0e0;
          display: flex;
          align-items: center;
          input {
            border: none;
            border-right: 1px solid #e0e0e0;
            box-sizing: border-box;
            width: 264px;
            height: 50px;
            padding-left: 14px;
          }
          a {
            @include bgImg(18px, 18px, "/imgs/icon-search.png");
            margin-left: 17px;
          }
        }
      }
    }
  }
}
</style>